<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <!--        <MyHeader @pushItem="receives"/>-->
        <MyHeader :pushItem="receives"/>
        <List :todos="todos"/>
        <MyFooter :todos="todos" :changeAll="changeAll" :clearDone="clearDone"/>
      </div>
    </div>
  </div>
</template>

<script>
import MyHeader from './part/Header'
import List from "@/components/todolist/part/List";
import MyFooter from "@/components/todolist/part/Footer";

import pubsub from 'pubsub-js'

export default {
  name: "Todolist",
  components: {
    List,
    MyHeader,
    MyFooter
  },
  data() {
    return {
      todos: JSON.parse(localStorage.getItem('todos')) || []
    }
  },
  watch: {
    todos: {
      deep: true,
      handler(newV) {
        console.log(9)
        if (!newV) {
          this.todos = []
          console.log(newV, 11)
        } else {
          localStorage.setItem('todos', JSON.stringify(newV))
        }
      }
    }
  },
  methods: {
    receives(x) {
      console.log(x)
      this.todos.unshift(x)
    },
    changeItem(id) {
      this.todos.forEach(s => {
        if (s.id === id) {
          s.done = !s.done
        }
      })
    },
    handleDel(id) {
      this.todos = this.todos.filter(s => s.id !== id)
    },
    changeAll(checked) {
      this.todos.forEach(s => {
        s.done = checked
      })
    },
    clearDone() {
      this.todos = this.todos.filter(s => !s.done)
    }
  },
  mounted() {
    //全局事件总线 回调函数中this指向vm而不是当前vc，需要使用()=>{}
    // this.$bus.$on('changeItem',this.changeItem)
    // this.$bus.$on('handleDel',this.handleDel)

    this.$bus.$on('handleEdit', (id, title) => {
      console.log(id, title)
     this.todos.forEach(s => {
        if (s.id === id) {
          s.title = title
        }
      })

    })

    //消息发布订阅 回调函数中this指向undefine，需要使用()=>{}
    this.pid1 = pubsub.subscribe('changeItem', (key, data) => {
      this.changeItem(data)
    })
    this.pid2 = pubsub.subscribe('handleDel', (key, data) => {
      this.handleDel(data)
    })
  },
  beforeDestroy() {
    // this.$bus.$off(['changeItem','handleDel'])

    this.$bus.$off('handleEdit')

    pubsub.unsubscribe(this.pid1)
    pubsub.unsubscribe(this.pid2)
  }
}
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-edit {
  color: white;
  background-color: skyblue;
  border: 1px solid #69baa0;
  margin-right: 5px;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}


</style>
